package com.caipiao.util;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import jodd.util.Base64;

public class CodeHelper
{
	public static byte[] Base64Decode(String oriData)
	{
		return Base64.decode(oriData.replace(" ", "").replace("\r\n", ""));
	}
	
	public static String Base64Encode(byte[] oriData)
	{
		return Base64.encodeToString(oriData).replace(" ", "").replace("\r\n", "");
	}
	
	//DES解密
	public static byte[] DesDecrypt(byte[] byteMi, byte[]  byteKey) throws Exception
	{
		return CodeHelper.Des(byteMi, byteKey, Cipher.DECRYPT_MODE);
	}
	
	//DES加密
	public static byte[] DesEncrypt(byte[] byteMi, byte[]  byteKey) throws Exception
	{
		
		return CodeHelper.Des(byteMi, byteKey, Cipher.ENCRYPT_MODE);
	}
	
	//DES加解密
	private static byte[] Des(byte[] byteData, byte[]  byteKey, int opmode) throws Exception
	{
		Cipher cipher = null;
		try
		{
    		SecretKeySpec desKey = new SecretKeySpec(byteKey, "DES");
    		
    		cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    		cipher.init(opmode, desKey);
    		
    		return cipher.doFinal(byteData);
		}
		finally
		{
			cipher = null;
		}	
	}
	
	//DES3解密
	public static byte[] Des3_ECBDecrypt(byte[] byteMi, byte[]  byteKey) 
				throws Exception
	{
		return CodeHelper.Des3_ECB(byteMi, byteKey, Cipher.DECRYPT_MODE);
	}
	
	//DES3加密
	public static byte[] Des3_ECBEncrypt(byte[] byteMi, byte[]  byteKey) 
		throws Exception
	{
		return CodeHelper.Des3_ECB(byteMi, byteKey, Cipher.ENCRYPT_MODE);
	}
	
	//DES3加解密 CBC
	private static byte[] Des3_ECB(byte[] byteData, byte[]  byteKey, int opmode) 
			throws Exception
	{
		Cipher cipher = null;
		try
		{
    		SecretKeySpec desKey = new SecretKeySpec(byteKey, "DESede");

    		cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    		cipher.init(opmode, desKey);
    		
    		return cipher.doFinal(byteData);
		}
		finally
		{
			cipher = null;
		}	
	}
	
	//DES3解密
	public static byte[] Des3_CBCDecrypt(byte[] byteMi, byte[]  byteKey, byte[] ips) 
				throws Exception
	{
		return CodeHelper.Des3_CBC(byteMi, byteKey, ips, Cipher.DECRYPT_MODE);
	}
	
	//DES3加密
	public static byte[] Des3_CBCEncrypt(byte[] byteMi, byte[]  byteKey, byte[] ips) 
		throws Exception
	{
		return CodeHelper.Des3_CBC(byteMi, byteKey, ips, Cipher.ENCRYPT_MODE);
	}
	
	//DES3加解密 CBC
	private static byte[] Des3_CBC(byte[] byteData, byte[]  byteKey, byte[] ips, int opmode) 
			throws Exception
	{
		Cipher cipher = null;
		try
		{
    		SecretKeySpec desKey = new SecretKeySpec(byteKey, "DESede");
    		IvParameterSpec ips_ = new IvParameterSpec(ips);
    		
    		cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    		cipher.init(opmode, desKey, ips_);
    		
    		return cipher.doFinal(byteData);
		}
		finally
		{
			cipher = null;
		}	
	}
	
	public static byte[] AesDecrypt(byte[] byteMi, byte[]  byteKey) throws Exception
	{
		return CodeHelper.Aes(byteMi, byteKey, Cipher.DECRYPT_MODE);
	}

	public static byte[] AesEncrypt(byte[] byteMi, byte[]  byteKey) throws Exception
	{
		return CodeHelper.Aes(byteMi, byteKey, Cipher.ENCRYPT_MODE);
	}

	private static byte[] Aes(byte[] byteData, byte[]  byteKey, int opmode) throws Exception
	{
		Cipher cipher = null;
		try
		{
			SecretKeySpec aesKey = new SecretKeySpec(byteKey, "AES");
			cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(opmode, aesKey);

			return cipher.doFinal(byteData);
		} 
		finally
		{
			cipher = null;
		}
	}

	public static byte[] RsaDecrypt(byte[] byteMi, Key pKey) throws Exception
	{
		return CodeHelper.Rsa(byteMi, pKey, Cipher.DECRYPT_MODE);
	}

	public static byte[] RsaEncrypt(byte[] byteMi, Key pKey) throws Exception
	{
		return CodeHelper.Rsa(byteMi, pKey, Cipher.ENCRYPT_MODE);
	}

	private static byte[] Rsa(byte[] byteData, Key pKey, int opmode) throws Exception
	{
		Cipher cipher = null;
		try
		{
			cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(opmode, pKey);

			return cipher.doFinal(byteData);
		}
		finally
		{
			cipher = null;
		}
	}

}
